package cn.hnc.checkapi.redis;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SequenceUtils {
   @Autowired
   private CacheService cacheService;
   private static final int DEFAULT_LENGTH = 9;

   public static String getSequence(long seq) {
      String str = String.valueOf(seq);
      int len = str.length();
      if (len >= 9) {
         return str;
      } else {
         int rest = 9 - len;
         StringBuilder sb = new StringBuilder();

         for(int i = 0; i < rest; ++i) {
            sb.append('0');
         }

         sb.append(str);
         return sb.toString();
      }
   }

   public String getAutoFlowCode() {
      String currentDate = (new SimpleDateFormat("yyyyMMdd")).format(new Date());
      Long num = this.cacheService.getIncrementNum("" + currentDate);
      String flowCode = getSequence(num);
      return flowCode;
   }
}
